﻿using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Geodatabase;
using System;
using System.Windows.Forms;

namespace School
{
    public partial class QueryBuilderForm : Form
    {
        public string BuiltExpression { get; private set; } = "";

        public QueryBuilderForm(IFeatureLayer featureLayer)
        {
            InitializeComponent();
            LoadFields(featureLayer);
        }

        private void LoadFields(IFeatureLayer featureLayer)
        {
            IFeatureClass featureClass = featureLayer.FeatureClass;
            for (int i = 0; i < featureClass.Fields.FieldCount; i++)
            {
                IField field = featureClass.Fields.get_Field(i);
                listBoxFields.Items.Add(field.Name);
            }
        }

        private void listBoxFields_DoubleClick(object sender, EventArgs e)
        {
            if (listBoxFields.SelectedItem != null)
            {
                textBoxExpression.Text += listBoxFields.SelectedItem.ToString() + " ";
            }
        }

        private void btnOperator_Click(object sender, EventArgs e)
        {
            Button btn = (Button)sender;
            textBoxExpression.Text += btn.Text + " ";
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            BuiltExpression = textBoxExpression.Text.Trim();
            DialogResult = DialogResult.OK;
            Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
            Close();
        }
    }
}